package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import view.EditTemplatePanel;
import view.ViewHelpers;
import model.AppModel;
import model.ModelException;
import model.Template;

/**
 * The controller for creating or editing templates
 * 
 * This controller has a reference to the main model, and the current view it is working on.
 * It assigns handlers to events for the cancel, update and add buttons and highlight of tokens
 * text area. It also specifies the actions to invoke upon triggering these events. 
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class EditTemplateController {
	
	
	private AppModel model;
	private Template template;
	private EditTemplatePanel view;

	/**
	 * Construting using the application model, the mode (create/update) and the template object)
	 * 
	 * @param model The application model
	
	 * @param template The template object, used if the mode is update
	 */
	public EditTemplateController(AppModel model, Template template){
		
		this.model = model;
		this.template = template;

		setupView();
		assignHandlers();
		showView();
	}

	/**
	 * Assigns handlers to different evnets on the view
	 */
	private void assignHandlers() {
		
		/**
		 * Body change listener to highlight the tokens
		 */
		view.getBody().getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				actionHighlightTokens();
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				actionHighlightTokens();
				
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				actionHighlightTokens();
			}
		});
		
		/**
		 * Cancel
		 */
		view.getCancelBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});
		
		/**
		 * Add
		 */
		view.getAddBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionAdd();
			}
		});
		
		/**
		 * update
		 */
		view.getEditBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionUpdate();
			}
		});
		
	}

	/**
	 * Highlights the tokens in the body
	 */
	protected void actionHighlightTokens() {
		
	}

	/**
	 * Update the template
	 */
	protected void actionUpdate() {
		
		String title = view.getTitle().getText().trim();
		String body = view.getBody().getText();
		
		if (inputsAreValid(title, body)){
			try {
				template.setBody(body);
				template.save();
			} catch (ModelException e) {
				ViewHelpers.alertError("The template could not be created, please try again later");
				e.printStackTrace();
			}
			ViewHelpers.disposeParentDialog(view);
		}
	}

	/**
	 * Add the template
	 */
	protected void actionAdd() {
		
		String title = view.getTitle().getText().trim();
		String body = view.getBody().getText();
		
		if (inputsAreValid(title, body)){
			try {
				model.getUser().createNewTemplate(title, body);
			} catch (ModelException e) {
				ViewHelpers.alertError("The template could not be created, please try again later");
				e.printStackTrace();
			}
			ViewHelpers.disposeParentDialog(view);
		}
		
	}

	/**
	 * Validates the inputs from the view
	 * 
	 * @param title The title for the template
	 * @param body The body for the template
	
	 * @return boolean
	 */
	private boolean inputsAreValid(String title, String body) {
		
		// Empty
		if (title.isEmpty() || body.isEmpty()){
			ViewHelpers.alertError("Title and body can not be empty");
			return false;
		}
		
		// If a new template, check for existing title
		if (template == null){
			for (Template template : model.getUser().getTemplates()){
				if (template.getTitle().trim().equalsIgnoreCase(title)){
					ViewHelpers.alertError("You already have a meeting with the same title, please use a different one");
					return false;
				}
			}
		}

		return true;
	}

	/**
	 * Cancel and close the view
	 */
	protected void actionCancel() {
		ViewHelpers.disposeParentDialog(view);
	}

	/**
	 * Shows the view with the appropriate mode
	 */
	private void showView() {
		
		if (template == null){
			ViewHelpers.showDialog(view, "Create new template");
		} else {
			ViewHelpers.showDialog(view, "Edit template: " + template.getTitle());
		}
	}

	/**
	 * Create the edit or create view with appropriate settings
	 */
	private void setupView() {
		
		view = new EditTemplatePanel();
		
		if (template == null){
			
			// New template
			view.setMode("create");
			
		} else {
			
			// Updating existing template
			view.setMode("update");
			view.getTitle().setText(template.getTitle());
			view.getBody().setText(template.getBody());
		}
	}
	
	
	
	
}
